package com.example.api;

import com.example.pojo.TbCart;
import com.example.service.TbCartService;
import com.example.util.Response;
import com.example.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program:bookstore2
 * @auther:linan
 * @date:2022/12/26
 **/
@RestController
//@RequestMapping("/cart")
public class CartAPI {
    @Autowired
    private TbCartService tbCartService;

    //查询购物车相关信息
    @GetMapping("/books")
    public ResponseResult<List<TbCart>> getAllBook() {
        List<TbCart> bookList = tbCartService.list();
//        return bookList;
        System.out.println(bookList);
        return Response.createOkResp(bookList);
    }

    @GetMapping("/booksByBook/{cid}")
    public ResponseResult<List<TbCart>> getbyBookId(@PathVariable("cid") Long cid) {
        List<TbCart> tbcart = tbCartService.selectByBookId(cid);
        return Response.createOkResp(tbcart);
    }

    @GetMapping("/booksByName/{bookName}")
    public ResponseResult<List<TbCart>> getbyBookName(@PathVariable("bookName") String name) {
        List<TbCart> bookList = tbCartService.selectByName(name);
        return Response.createOkResp(bookList);
    }


    @PostMapping("/cart")
    public ResponseResult<TbCart> addBook(TbCart tbCart) {
        try {
            int num = tbCartService.add(tbCart);
            System.out.println(num);
            return Response.createOkResp("添加成功");
        } catch (Exception e) {
            return Response.createFailResp("添加失败");
        }
    }

    @PutMapping("/books")
    public ResponseResult<Map> update(TbCart tbCart) {
        try {
            int num = tbCartService.update(tbCart);
            System.out.println(num);
            return Response.createOkResp("修改成功");
        } catch (Exception e) {
            return Response.createFailResp("修改失败");
        }
    }



}
